"""
EmailQA_SOP Service Layer
"""

from typing import List, Optional
from ..domain.emailqa_sop import EmailQA_SOP
from ..domain.emailqa_sop_repository import EmailQA_SOPRepository

class EmailQA_SOPService:
    """Service layer for EmailQA_SOP business logic"""
    
    def __init__(self, repository: EmailQA_SOPRepository):
        self.repository = repository
    
    def create_association(self, emailqa_id: int, sop_id: int, created_by: str) -> EmailQA_SOP:
        """Create a new EmailQA_SOP association"""
        if not emailqa_id or emailqa_id <= 0:
            raise ValueError("emailqa_id must be a positive integer")
        
        if not sop_id or sop_id <= 0:
            raise ValueError("sop_id must be a positive integer")
        
        if not created_by or not created_by.strip():
            raise ValueError("created_by is required and cannot be empty")
        
        emailqa_sop = EmailQA_SOP(
            emailqa_id=emailqa_id,
            sop_id=sop_id,
            created_by=created_by.strip()
        )
        
        return self.repository.create(emailqa_sop)
    
    def get_association_by_id(self, association_id: int) -> Optional[EmailQA_SOP]:
        """Get association by ID"""
        if not association_id or association_id <= 0:
            raise ValueError("association_id must be a positive integer")
        
        return self.repository.get_by_id(association_id)
    
    def get_all_associations(self) -> List[EmailQA_SOP]:
        """Get all associations"""
        return self.repository.get_all()
    
    def get_associations_as_dict_list(self) -> List[dict]:
        """Get all associations as list of dictionaries"""
        associations = self.repository.get_all()
        return [assoc.to_dict() for assoc in associations]
    
    def update_association(self, association_id: int, emailqa_id: int = None, sop_id: int = None, created_by: str = None) -> EmailQA_SOP:
        """Update an existing association"""
        if not association_id or association_id <= 0:
            raise ValueError("association_id must be a positive integer")
        
        # Get existing association
        existing = self.repository.get_by_id(association_id)
        if not existing:
            raise ValueError(f"Association with ID {association_id} not found")
        
        # Update fields if provided
        if emailqa_id is not None:
            if emailqa_id <= 0:
                raise ValueError("emailqa_id must be a positive integer")
            existing.emailqa_id = emailqa_id
        
        if sop_id is not None:
            if sop_id <= 0:
                raise ValueError("sop_id must be a positive integer")
            existing.sop_id = sop_id
        
        if created_by is not None:
            if not created_by.strip():
                raise ValueError("created_by cannot be empty")
            existing.created_by = created_by.strip()
        
        return self.repository.update(existing)
    
    def delete_association(self, association_id: int) -> bool:
        """Delete an association by ID"""
        if not association_id or association_id <= 0:
            raise ValueError("association_id must be a positive integer")
        
        return self.repository.delete(association_id)
    
    def get_associations_by_emailqa_id(self, emailqa_id: int) -> List[EmailQA_SOP]:
        """Get all associations for a specific EmailQA"""
        if not emailqa_id or emailqa_id <= 0:
            raise ValueError("emailqa_id must be a positive integer")
        
        return self.repository.get_by_emailqa_id(emailqa_id)
    
    def get_associations_by_sop_id(self, sop_id: int) -> List[EmailQA_SOP]:
        """Get all associations for a specific SOP"""
        if not sop_id or sop_id <= 0:
            raise ValueError("sop_id must be a positive integer")
        
        return self.repository.get_by_sop_id(sop_id)
    
    def get_sops_with_emailqa_by_subject(self, subject: str) -> List[dict]:
        """Get SOPs with associated EmailQA data by subject"""
        if not subject or not subject.strip():
            raise ValueError("subject is required and cannot be empty")
        
        return self.repository.get_sops_with_emailqa_by_subject(subject.strip())
    
    def association_exists(self, emailqa_id: int, sop_id: int) -> bool:
        """Check if an association exists"""
        if not emailqa_id or emailqa_id <= 0:
            raise ValueError("emailqa_id must be a positive integer")
        
        if not sop_id or sop_id <= 0:
            raise ValueError("sop_id must be a positive integer")
        
        return self.repository.association_exists(emailqa_id, sop_id)
    
    def get_association_by_id_as_dict(self, association_id: int) -> Optional[dict]:
        """Get association by ID as dictionary"""
        association = self.get_association_by_id(association_id)
        return association.to_dict() if association else None